import type {
	MesWorkOrder,
	MesWorkOrderBomItem,
	MesWorkOrderBomSubstitute,
	MesWorkOrderProcess,
} from "@/pages/mes/workOrder/entity";
import apiClient from "../../apiClient";
import React from "react";
import { WorkOrderKitCheck } from "@/pages/mes/workOrder/entity";

export interface PageListReq {
	//工单编码
	workOrderCode?: string;
	//工单类型
	workOrderType?: string;
	//关联产品物料ID
	productId?: number;
	//来源类型
	orderSource?: string;
	//产线ID
	lineId?: number;
	//父工单
	parentId?: number;
	//单据状态
	status?: string;
	//当前页码
	page: number;
	//每页数量
	pageSize: number;
}

export interface PageListRes {
	//总记录数
	total: number;
	//数据列表
	records: MesWorkOrder[];
}

const pageList = (data: PageListReq): Promise<PageListRes> =>
	apiClient.post({
		url: "/mes/work/order/pageList",
		data,
	});
const getInfo = (id: React.Key): Promise<MesWorkOrder> =>
	apiClient.get({
		url: `/mes/work/order/${id}`,
	});

const add = (data: MesWorkOrder): Promise<MesWorkOrder> =>
	apiClient.post({
		url: "/mes/work/order",
		data,
	});

const update = (data: MesWorkOrder): Promise<MesWorkOrder> =>
	apiClient.put({
		url: "/mes/work/order",
		data,
	});

const remove = (ids: React.Key[]): Promise<void> =>
	apiClient.delete({
		url: "/mes/work/order",
		params: {
			ids: ids.join(","),
		},
	});
const getItems = (id: React.Key): Promise<MesWorkOrderBomItem[]> =>
	apiClient.post({
		url: "/mes/work/order/item/list",
		data: {
			workOrderId: id,
		},
	});

const getSubstitutes = (id: React.Key): Promise<MesWorkOrderBomSubstitute[]> =>
	apiClient.post({
		url: "/mes/work/order/substitute/list",
		data: {
			workOrderId: id,
		},
	});

const getProcesss = (id: React.Key): Promise<MesWorkOrderProcess[]> =>
	apiClient.post({
		url: "/mes/work/order/process/list",
		data: {
			workOrderId: id,
		},
	});

// 工单拆分
const split = (id: React.Key, splitQty: number): Promise<void> =>
	apiClient.post({
		url: "/mes/work/order/split",
		params: {
			id: id,
			splitQty: splitQty,
		},
	});

// 齐套分析
const kitCheck = (workOrderId: React.Key): Promise<WorkOrderKitCheck[]> =>
	apiClient.get({
		url: `/mes/work/order/item/kitCheck/${workOrderId}`,
	});

// 工单下达
const transmit = (data: MesWorkOrder): Promise<MesWorkOrder> =>
	apiClient.put({
		url: "/mes/work/order/transmit",
		data,
	});

export const mesWorkOrderService = {
	pageList,
	getInfo,
	add,
	update,
	remove,
	getitems: getItems,
	getsubstitutes: getSubstitutes,
	getprocesss: getProcesss,
	split,
	kitCheck,
	transmit,
};

export default mesWorkOrderService;
